<template>
	<view>
		<uni-list class="list">
			<uni-list-item
				v-for="(log, logIndex) in logList"
				:key="log.logId" 
				@click="handleLogClick(log)"
				link>
				<template v-slot:body>
					<CREATE v-if="log.defectLogType==='CREATE'" :log="log"></CREATE>
					<IMPORT v-else-if="log.defectLogType==='IMPORT'" :log="log"></IMPORT>
					<ASSIGN v-else-if="log.defectLogType==='ASSIGN'" :log="log"></ASSIGN>
					<REJECTED v-else-if="log.defectLogType==='REJECTED'" :log="log"></REJECTED>
					<REPAIR v-else-if="log.defectLogType==='REPAIR'" :log="log"></REPAIR>
					<PASS v-else-if="log.defectLogType==='PASS'" :log="log"></PASS>
					<OPEN v-else-if="log.defectLogType==='OPEN'" :log="log"></OPEN>
					<CLOSED v-else-if="log.defectLogType==='CLOSED'" :log="log"></CLOSED>
					<UPDATE v-else-if="log.defectLogType==='UPDATE'" :log="log"></UPDATE>
					<view>
					  <button v-if="commentPermi" icon="el-icon-chat-line-round" size="mini" type="text" @click="showCommentInputHandle(log,logIndex)">{{$t('comment')}}</button>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
		<uni-load-more :status="loadStatus" @clickLoadMore="getLog({})" />
	</view>
</template>

<script>
	import {listLog} from "@/api/system/log";
	import {checkPermi} from "@/utils/permission";
	import CREATE from "./create.vue";
	import IMPORT from "./import.vue"
	import ASSIGN from "./assign.vue"
	import REJECTED from "./reject.vue";
	import REPAIR from "./repair.vue";
	import PASS from "./pass.vue";
	import CLOSED from "./close.vue";
	import OPEN from "./open.vue";
	import UPDATE from "./update.vue";
	export default {
		name: 'DefectLog',
		components: { CREATE,IMPORT,ASSIGN,REJECTED,REPAIR,PASS,CLOSED,OPEN,UPDATE },
		data() {
			return {
				// 显示的加载状态
				loadStatus: 'more',
				// 日志列表
				logList: [],
				// 列表中缺陷日志总数量
				total: 0,
				// 查询参数
				params: {
					pageNum: 1,
					pageSize: 10,
					defectId: null
				}
			}
		},
		props: {
			defect: {
			  type: Object,
			  default: ()=>{}
			},
			showComment: {
				type: Boolean,
				default: false
			}
		},
		watch: {
			defect: function(n, o){
				if(n!==o) {
					this.getLog(n);
				}
			}
		},
		computed: {
			commentPermi: function() {
			    return this.showComment && checkPermi(['system:defect:list']);
			}
		},
		methods: {
			getLog(defect) {
				this.params.defectId = defect.defectId || this.defect.defectId;
				listLog(this.params).then(res=>{
					this.logList = res.rows.map(l=>{
					  l.visibleCommentInput = false;
					  return l;
					});
					if(this.total > this.logList.length) {
						this.loadStatus = 'more';
						this.params.pageNum += 1;
					} else {
						this.loadStatus = 'no-more';
					}
					this.$emit('load');
				});
			},
			handleLogClick(log) {
				uni.navigateTo({
					url: '/subpackage/pages/system/defect/defect-log-detail?defectLogId='+log.defectLogId
				});
			}
		}
	}
</script>

<style>
</style>